package huixue.cn.com.rxapp.rx;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.ImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import huixue.cn.com.library.util.LogUtils;
import huixue.cn.com.library.util.PixelUtils;
import huixue.cn.com.library.util.ToastUtils;
import huixue.cn.com.rxapp.R;
import huixue.cn.com.rxapp.base.SimpleActivity;

/**
 * 老照片效果
 * 0.393 0.769 0.189 0 0
 * 0.349 0.686 0.168 0 0
 * 0.272 0.534 0.131 0 0
 * 0 0 0 0 0
 */
public class BitmapColor2Activity extends SimpleActivity {

    @BindView(R.id.iv_timg)
    ImageView mIvTimg;

    @BindView(R.id.group)
    GridLayout gridLayout;

    Bitmap mBitmap;
    @BindView(R.id.btn1)
    Button btn1;
    @BindView(R.id.btn2)
    Button btn2;

    private Bitmap originBmp;


    private EditText[] editText = new EditText[20];

    private int etWidth, etHeight;

    //private ColorMatrix colorMatrix = new ColorMatrix

    private float[] f = new float[20];

    @Override
    public int getLayout() {
        return R.layout.activity_bitmap_color2;
    }

    @Override
    public void initView() {
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.timg);
        float scale = PixelUtils.getScreenWidth() * 1.0f / mBitmap.getWidth();
        Matrix matrix = new Matrix();
        matrix.setScale(scale, scale);
        originBmp = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);

        mIvTimg.setImageBitmap(originBmp);
    }

    @Override
    public void addEvent() {
        gridLayout.post(() -> {
            etWidth = gridLayout.getWidth() / 5;
            etHeight = gridLayout.getHeight() / 4;
            LogUtils.d("BitmapColor2Activity", "etWidth==" + etWidth + "==etHeight==" + etHeight);
            for (int i = 0; i < editText.length; i++) {
                editText[i] = new EditText(BitmapColor2Activity.this);
                gridLayout.addView(editText[i], etWidth, etHeight);
                if (i % 6 == 0) {
                    editText[i].setText("1");
                } else {
                    editText[i].setText("0");
                }
            }
        });
    }

    private void loadMatrixValue() {
        for (int i = 0; i < editText.length; i++) {
            f[i] = Float.valueOf(editText[i].getText().toString());
        }
    }


    private void confirmClick() {
        loadMatrixValue();

        setImageBitmapByColorMatrix();
    }

    private void setImageBitmapByColorMatrix() {

        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.set(f);

        Bitmap bmp = Bitmap.createBitmap(originBmp.getWidth(), originBmp.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

        canvas.drawBitmap(originBmp, 0, 0, paint);

        mIvTimg.setImageBitmap(bmp);
    }


    @OnClick({R.id.btn1, R.id.btn2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                break;
            case R.id.btn2:
                ToastUtils.showToast("btn2");
                confirmClick();
                break;
        }
    }
}
